# coding=utf8
# 导入工程的warname字段
import requests

from app import create_app
from app.modules.project.models import ProjectInfo
from config import BaseConfig
from app.modules.common.utils import recursive_json_loads, DbCommit

application = create_app(flask_config = 'local')


def main(_token_id):
    with application.app_context():
        _list = ProjectInfo.query.filter_by(isOnBeetle = '0').all()
        for _pro in _list:
            _url = "%s/project/getProject?tokenId=%s&projectName=%s" % (
                BaseConfig.BEETLE_API_URL, _token_id, _pro.trunk_name)
            _rtn = recursive_json_loads(requests.post(url = _url).content)
            if _rtn.code != 0:
                print "获取工程 %s 信息失败,error:%s" % (_pro.project_name, _rtn.desc)
            else:
                print "%s . warname is %s" % (_pro.project_name, _rtn.data.warName)
                _pro.package_name = _rtn.data.warName
                DbCommit(cleanPro = True)
                pass


if __name__ == '__main__':
    main("334f7c3c1fd511e7990f005056b17b2e")
    pass
